package com.lcf.system.manager;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.base.exception.SysException;
import com.lcf.system.dto.dict.SysDictTypeDTO;
import com.lcf.framework.model.system.SysDictType;
import org.springframework.stereotype.Component;

/**
 * 字典类型
 * @author lcf
 */
@Component
public class SysDictTypeManager {
    /**
     * 检查是否已存在
     */
    public void checkExist(Long id, SysDictTypeDTO dto) {
        boolean exists = Db.lambdaQuery(SysDictType.class)
                .ne(id != null, SysDictType::getId, id)
                .and(x -> x.eq(SysDictType::getTypeCode, dto.getTypeCode())
                        .or().eq(SysDictType::getTypeName, dto.getTypeName())).exists();

        if (exists) {
            throw new SysException("字典类型已存在");
        }
    }

    /**
     * 检查id
     */
    public SysDictType checkId(long id) {
        SysDictType sysDictType = Db.lambdaQuery(SysDictType.class).eq(SysDictType::getId, id).one();
        if (sysDictType == null) {
            throw new SysException("字典类型不存在");
        }

        return sysDictType;
    }
}
